package com.example.service;

import com.example.entity.UserTask;
import com.example.mapper.UserTaskMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
帖子业务处理
 */
@Service
public class UserTaskService {

    @Resource
    private UserTaskMapper userTaskMapper;

    public void add(UserTask userTask) {
        userTaskMapper.insert(userTask);
    }

    public void deleteById(Integer id) {
        userTaskMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            this.deleteById(id);
        }
    }

    public void updateById(UserTask userTask) {
        UserTask selfUserTask=new UserTask();
        selfUserTask.setUid(userTask.getUid());
        selfUserTask.setRid(userTask.getRid());
        selfUserTask.setCreatetime(userTask.getCreatetime());
        List<UserTask> theUserTaskList = selectAll(selfUserTask);
        UserTask theUserTask =theUserTaskList.size()!=0?theUserTaskList.get(0):null;
        try
        {
            if (((userTask.getStatus().equals("1")) && (theUserTask.getStatus().equals("0")) || userTask.getStatus().equals("0") || userTask.getStatus().equals("2")))
            {
                userTaskMapper.updateById(userTask);
            }
        }catch (Exception e)
        {
            userTaskMapper.updateById(userTask);
        }
    }

    public UserTask selectById(Integer id) {
        return userTaskMapper.selectById(id);
    }

    public List<UserTask> selectAll(UserTask userTask) {
        return userTaskMapper.selectAll(userTask);
    }

    public PageInfo<UserTask> selectPage(UserTask userTask, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserTask> userTaskList = userTaskMapper.selectAll(userTask);
        return PageInfo.of(userTaskList);
    }
}
